/**
 * Filename: OfficialAlbumService.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/8 18:50
 */
package com.github.postsanf.yinian.service;

import android.content.Intent;
import android.util.Log;

import com.github.postsanf.yinian.BaseIntentService;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.constants.YNSingleton;
import com.github.postsanf.yinian.entity.YNAlbumResponse;
import com.github.postsanf.yinian.utils.ReqUtils;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;

import java.util.LinkedHashMap;

/**
 * 官方相册后台服务
 */
public class OfficialAlbumService extends BaseIntentService {

    public OfficialAlbumService() {
        super("OfficialAlbumService");
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            final String action = intent.getAction();
            switch (action) {
                case ACTIONs.actionGetOfficialAlbum:
                    getAlbumListById();
                    break;
                case ACTIONs.actionGetNewStudent:
                    getNewStudentAlbumListById();
                    break;
            }
        }
    }

    // 获取官方相册信息
    private void getAlbumListById() {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_USER_ID, String.valueOf(application.getCurID()));
        StringRequest req = new StringRequest(getString(R.string.BASE_URL) + getString(R.string.ynShowAllOfficialAlbum))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        YNAlbumResponse resOfficialGroup = gson.fromJson(s, YNAlbumResponse.class);
                        if (resOfficialGroup != null && ReqUtils.isSuccess(resOfficialGroup.getCode())) {
                            YNSingleton.getInstance().getOfficialAlbumList().addAll(resOfficialGroup.getData());
                            // 通知官方相册更新
                            Intent upIntent = new Intent(ACTIONs.actionUpdateOfficialAlbum);
                            sendBroadcast(upIntent);
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }

    // 获取官方相册信息
    private void getNewStudentAlbumListById() {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_USER_ID, String.valueOf(application.getCurID()));
        StringRequest req = new StringRequest(getString(R.string.BASE_URL) + getString(R.string.ynShowNewStudentAlbum))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        YNAlbumResponse resOfficialGroup = gson.fromJson(s, YNAlbumResponse.class);
                        if (resOfficialGroup != null && ReqUtils.isSuccess(resOfficialGroup.getCode())) {
                            YNSingleton.getInstance().getmNewStudentAlbumList().addAll(resOfficialGroup.getData());
                            // 通知官方相册更新
                            Intent upIntent = new Intent(ACTIONs.actionUpdateNewStudent);
                            sendBroadcast(upIntent);
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
